package com.jeff.android.lab.module.thirdwidget.tangram;

import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;

import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.tmall.wireless.tangram.TangramBuilder;
import com.tmall.wireless.tangram.TangramEngine;
import com.tmall.wireless.tangram.util.IInnerImageSetter;

import butterknife.BindView;

/**
 * @author: duff
 * @date: 2019/3/15
 * @since: 1.0.0
 */
public class TangramFragment extends BaseFragment {

    @BindView(R.id.rlv)
    RecyclerView mRlv;

    TangramBuilder.InnerBuilder builder;
    TangramEngine engine;

    @Override
    protected int getRootLayout() {
        return R.layout.thirdwidget_fragment_tangram;
    }

    @Override
    protected void initView() {
        super.initView();

        // 初始化 Tangram 环境
        TangramBuilder.init(mContext, new IInnerImageSetter() {
            @Override
            public <IMAGE extends ImageView> void doLoadImageUrl(IMAGE view, String url) {

            }
        }, ImageView.class);

        // 初始化 TangramBuilder
        builder = TangramBuilder.newInnerBuilder(mContext);


        // 生成TangramEngine实例
        engine = builder.build();


        // 绑定 recyclerView
        engine.bindView(mRlv);

        // 监听 recyclerView 的滚动事件
        mRlv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                engine.onScrolled();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        engine.destroy();
    }

}
